package org.example.ui.customer;

import org.example.manage.MilkTeaManage;
import org.example.model.MilkTea;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class HomePanel extends JPanel {
    private ImageIcon[] icons;
    private List<MilkTea> list;
    private JLabel[] label = new JLabel[4];
    private int x = 0, y = 1, z = 2;
    public HomePanel(){
        setLayout(null);
        for (int i = 0; i < 3; i++) {
            label[i] = new JLabel("");
        }
        set();
        label[3].setFont(new Font("", Font.BOLD, 40));
        label[0].setBounds(100, 580, 200, 40);
        label[1].setBounds(450, 580, 200,40);
        label[2].setBounds(800, 580, 200, 40);
        label[3].setBounds(420, 100, 200, 100);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(5000);
                        set();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    if (z < list.size() - 1) {
                        x++;
                        y++;
                        z++;
                    }
                    else {
                        x = 0;
                        y = 1;
                        z = 2;
                    }
                }
            }
        }).start();
    }
    private void set() {
        list = new MilkTeaManage().read();
        if (list.size()>=3) {
            icons = new ImageIcon[list.size()];
        }
        else {
            icons = new ImageIcon[3];
        }
        for (int i = 0; i < list.size(); i++) {
            icons[i] = new ImageIcon(list.get(i).imagePath);
        }
        label[3] = new JLabel("奶茶推荐");
        for (int i = 0; i < 4; i++) {
            if (i < 3) {
                label[i].setFont(new Font("", Font.BOLD, 20));
            }
            add(label[i]);
        }
    }
    @Override
    public void paintComponent(Graphics g) {
        ImageIcon imageIcon = new ImageIcon("data/background/屏幕截图 2024-12-17 124832.png");
        g.drawImage(imageIcon.getImage(), 0, 0, getWidth(), getHeight(), this);
        if (list.size()>=3) {
            g.drawImage(icons[x].getImage(), 50, 300, 250, 250, this);
            g.drawImage(icons[y].getImage(), 400, 300, 250, 250, this);
            g.drawImage(icons[z].getImage(), 750, 300, 250, 250, this);
            label[0].setText(list.get(x).milkName);
            label[1].setText(list.get(y).milkName);
            label[2].setText(list.get(z).milkName);
        }
        else {
            if (list.size() == 1) {
                g.drawImage(icons[x].getImage(), 50, 300, 250, 250, this);
                label[0].setText(list.get(x).milkName);
                label[1].setText("");
                label[2].setText("");
            }
            if (list.size() == 2) {
                g.drawImage(icons[x].getImage(), 50, 300, 250, 250, this);
                g.drawImage(icons[y].getImage(), 400, 300, 250, 250, this);
                label[0].setText(list.get(x).milkName);
                label[1].setText(list.get(y).milkName);
                label[2].setText("");
            }
            else {
                label[0].setText("");
            }
        }
    }
}
